using System;
using System.IO;
using System.Linq;
using System.Text;
using mb;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;

public class StartOption : API.IStartOption
{

    static string configPath = "/../../tool/.ucmd.json";
    static string tmpConfigPath = "/../../tool/player.json";
    // /////////////////////////////////////////////
    JObject startOption = new JObject();
    ILog logger = mb.ServiceLocator.Instance.Get<ILog>();

    public T GetValue<T>(string key)
    {
        return startOption.Value<T>(key);
    }
    public string GetValue(string key)
    {
        return startOption.Value<string>(key);
    }
    public string GetStringValue(string key)
    {
        return startOption.Value<string>(key);

    }
    public int GetIntValue(string key)
    {
        return startOption.Value<int>(key);
    }
    public bool GetBoolValue(string key)
    {
        return startOption.Value<bool>(key);
    }

    public API.IStartOption SetValue<T>(string key, T value)
    {
        startOption[key] = JToken.FromObject(value);
        return this;
    }

    public void SetTmpOption(JObject option)
    {
        File.WriteAllText(tmpConfigPath, option.ToString());
    }

    public bool HasTmpOption()
    {
        return File.Exists(tmpConfigPath);
    }
    public void LoadStartOption()
    {
        try
        {
#if UNITY_EDITOR
            if (File.Exists(configPath))
            {
                startOption = JObject.Parse(File.ReadAllText(configPath));
            }

            if (File.Exists(tmpConfigPath))
            {
                startOption.Merge(JObject.Parse(File.ReadAllText(tmpConfigPath)));
                File.Delete(tmpConfigPath);
            }

            Log.Logs("configPath", configPath, tmpConfigPath, startOption);
#endif

            string encodedString = null;

#if __ANDROID__
            encodedString = mb.Droid.StartActivity.This.Intent.GetStringExtra("StartParam");

#elif __IOS__
            // string[] args = Foundation.NSProcessInfo.ProcessInfo.Arguments.Skip(1).Take(10).ToArray<string>();
            string[] args = Foundation.NSProcessInfo.ProcessInfo.Arguments;
            logger.Log("ios start argments  :", args.Length);
            foreach (var item in args)
            {
                logger.Log("arg", item);
            }
            if (args.Length == 2)
                encodedString = args[1];
            else if (args.Length > 2)
            {
                var prefix = "-app-arg";
                for (int i = 1; i < args.Length; i++)
                    if (args[i].StartsWith(prefix))
                    {
                        encodedString = args[i].Substring(prefix.Length + 1);
                        break;
                    }
            }
#endif

            if (encodedString != null)
            {
                byte[] data = Convert.FromBase64String(encodedString);
                string decodedString = Encoding.UTF8.GetString(data);
                startOption.Merge(JObject.Parse(decodedString));
            }

            logger.Log("load start option  :", startOption.ToString());

        }
        catch (Exception e)
        {
            logger.Log(e);
        }
    }
}